package com.zhuo.project.order.controller;

/**
 * @author fuhao  <fuhao@kuaishou.com>
 * Created on 2020-04-04
 */
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zhuo.common.constant.RedisKeyConstants;
import com.zhuo.common.utils.SecurityUtils;
import com.zhuo.framework.aspectj.lang.annotation.Log;
import com.zhuo.framework.aspectj.lang.enums.BusinessType;
import com.zhuo.framework.redis.RedisCache;
import com.zhuo.framework.web.controller.BaseController;
import com.zhuo.framework.web.domain.AjaxResult;
import com.zhuo.common.utils.poi.ExcelUtil;
import com.zhuo.framework.web.page.TableDataInfo;
import com.zhuo.project.order.domain.ZzDiagnoseHead;
import com.zhuo.project.order.domain.ZzDiagnoseHeadDto;
import com.zhuo.project.order.service.IZzDiagnoseHeadService;
import com.zhuo.project.system.domain.SysDept;
import com.zhuo.project.system.service.ISysDeptService;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

@Api("杂项操作")
@RestController
@RequestMapping("/sundry/head")
public class ZzSundryHeadController extends BaseController
{
    @Autowired
    private IZzDiagnoseHeadService zzDiagnoseHeadService;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private ISysDeptService deptService;

    /**
     * 查询处方单列表
     */
    @ApiOperation("获取杂项列表")
    @ApiImplicitParam(name = "zzDiagnoseHeadDto", value = "获取杂项列表", dataType = "ZzDiagnoseHeadDto")
    @PreAuthorize("@ss.hasPermi('sundry:head:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZzDiagnoseHeadDto zzDiagnoseHeadDto)
    {
        String deptIds = zzDiagnoseHeadDto.getDeptIds();
        List<String> deptList = new ArrayList<>();
        if(StrUtil.isBlank(deptIds)){
            Long deptId = SecurityUtils.getLoginUser().getUser().getDeptId();
            SysDept sysDeptSelect = deptService.selectDeptById(deptId);
            String deptCode = sysDeptSelect.getDeptCode();
            List<SysDept> deptSelect = deptService.buildChildDeptListByDeptCode(deptCode);
            deptList = deptSelect.stream().filter(sysDept -> StrUtil.isNotEmpty(sysDept.getDeptCode()))
                    .map(SysDept::getDeptCode).collect(Collectors.toList());
        }else {
            String []  deptIdList= deptIds.split(",");
            for (String deptId :deptIdList) {
                SysDept sysDeptSelect = deptService.selectDeptById(Long.parseLong(deptId));
                String deptCode = sysDeptSelect.getDeptCode();
                if(StrUtil.isNotBlank(deptCode)){
                    deptList.add(deptCode);
                }
            }
        }

       /* if(StrUtil.isNotBlank(zzDiagnoseHeadDto.getAuditStatusStr())){
            String []  auditStatusStrList = zzDiagnoseHeadDto.getAuditStatusStr().split(",");
            List<Integer> auditStatusList = new ArrayList<>(auditStatusStrList.length);
            for (String auditStatus : auditStatusStrList) {
                auditStatusList.add(Integer.parseInt(auditStatus));
            }
            zzDiagnoseHeadDto.setAuditStatusList(auditStatusList);
        }*/

        if(StrUtil.isNotBlank(zzDiagnoseHeadDto.getIsEquipStr())){
            String []  isEquipStrList = zzDiagnoseHeadDto.getIsEquipStr().split(",");
            List<Integer> isEquipList = new ArrayList<>(isEquipStrList.length);
            for (String isEquip : isEquipStrList) {
                isEquipList.add(Integer.parseInt(isEquip));
            }
            zzDiagnoseHeadDto.setIsEquipList(isEquipList);
        }

        if(StrUtil.isNotBlank(zzDiagnoseHeadDto.getIsSendStr())){
            String []  isSendStrList = zzDiagnoseHeadDto.getIsSendStr().split(",");
            List<Integer> isSendList = new ArrayList<>(isSendStrList.length);
            for (String isSend : isSendStrList) {
                isSendList.add(Integer.parseInt(isSend));
            }
            zzDiagnoseHeadDto.setIsSendList(isSendList);
        }

        if(StrUtil.isNotBlank(zzDiagnoseHeadDto.getIsBillStr())){
            String []  isBillStrList = zzDiagnoseHeadDto.getIsBillStr().split(",");
            List<Integer> isBillList = new ArrayList<>(isBillStrList.length);
            for (String isBill : isBillStrList) {
                isBillList.add(Integer.parseInt(isBill));
            }
            zzDiagnoseHeadDto.setIsBillList(isBillList);
        }
        zzDiagnoseHeadDto.setDepts(deptList);
        zzDiagnoseHeadDto.setOrderType(2);
        startPage();
        List<ZzDiagnoseHead> list = zzDiagnoseHeadService.selectZzDiagnoseHeadList(zzDiagnoseHeadDto);
        return getDataTable(list);
    }

    /**
     * 导出杂项单列表
     */
    @ApiOperation("导出杂项单列表")
    @ApiImplicitParam(name = "zzDiagnoseHeadDto", value = "导出杂项单列表", dataType = "ZzDiagnoseHeadDto")
    @PreAuthorize("@ss.hasPermi('sundry:head:export')")
    @Log(title = "杂项单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ZzDiagnoseHeadDto zzDiagnoseHeadDto)
    {
        String deptCode = zzDiagnoseHeadDto.getDeptCode();
        List<SysDept> deptSelect = deptService.buildChildDeptListByDeptCode(deptCode);
        List<String> deptList = deptSelect.stream().filter(sysDept -> StrUtil.isNotEmpty(sysDept.getDeptCode()))
                .map(SysDept::getDeptCode).collect(Collectors.toList());
        zzDiagnoseHeadDto.setDepts(deptList);
        zzDiagnoseHeadDto.setOrderType(2);
        List<ZzDiagnoseHead> list = zzDiagnoseHeadService.selectZzDiagnoseHeadList(zzDiagnoseHeadDto);
        ExcelUtil<ZzDiagnoseHead> util = new ExcelUtil<ZzDiagnoseHead>(ZzDiagnoseHead.class);
        return util.exportExcel(list, "head");
    }

    /**
     * 获取处方单详细信息
     */
    @PreAuthorize("@ss.hasPermi('sundry:head:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(zzDiagnoseHeadService.selectZzDiagnoseHeadById(id));
    }

    /**
     * 新增处方单
     */
    @PreAuthorize("@ss.hasPermi('sundry:head:add')")
    @Log(title = "处方单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZzDiagnoseHead zzDiagnoseHead)
    {
        return toAjax(zzDiagnoseHeadService.insertZzDiagnoseHead(zzDiagnoseHead));
    }

    /**
     * 保存查询条件
     */
    @ApiOperation("保存杂项查询条件")
    @ApiImplicitParam(name = "zzDiagnoseHeadDto", value = "保存杂项查询条件", dataType = "ZzDiagnoseHeadDto")
    @PreAuthorize("@ss.hasPermi('sundry:head:list')")
    @Log(title = "杂项单", businessType = BusinessType.INSERT)
    @PostMapping(value = "/addQueryParam")
    public AjaxResult addQueryParam(@RequestBody ZzDiagnoseHeadDto zzDiagnoseHeadDto) {
        String userName = SecurityUtils.getUsername();
        String userQueryKey = MessageFormat
                .format(RedisKeyConstants.SUNDRY_QUERY_PARAM_KEY, userName);
        logger.info("请求参数：" + JSONUtil.toJsonStr(zzDiagnoseHeadDto) );
        redisCache.setCacheObject(userQueryKey, JSONUtil.toJsonStr(zzDiagnoseHeadDto));
        return toAjax(1);
    }

    /**
     * 获取处方单查询条件
     */
    @ApiOperation("查询杂项查询条件")
    @PreAuthorize("@ss.hasPermi('sundry:head:list')")
    @GetMapping(value = "/getQueryParam")
    public AjaxResult getQueryInfo() {
        String userName = SecurityUtils.getUsername();
        String userQueryKey = MessageFormat
                .format(RedisKeyConstants.SUNDRY_QUERY_PARAM_KEY, userName);
        String queryParam = redisCache.getCacheObject(userQueryKey);
        return AjaxResult.success(queryParam);
    }

    /**
     * 修改处方单
     */
    @PreAuthorize("@ss.hasPermi('sundry:head:edit')")
    @Log(title = "处方单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZzDiagnoseHead zzDiagnoseHead)
    {
        return toAjax(zzDiagnoseHeadService.updateZzDiagnoseHead(zzDiagnoseHead));
    }

    /**
     * 删除处方单
     */
    @PreAuthorize("@ss.hasPermi('sundry:head:remove')")
    @Log(title = "处方单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(zzDiagnoseHeadService.deleteZzDiagnoseHeadByIds(ids));
    }






}